<?php

if (!defined('ROOT'))
    die();

class Adminarea_minisqladmin {

    public function init() {

	$c = '';
	$_POST['query'] = isset($_POST['query']) ? $_POST['query'] : '';

	if (isset($_POST['query']) && !empty($_POST['query'])) {
	    $resultTable = '';
	    $head = false;
	    $q = Mysql::getInstance()->query($_POST['query']);

	    while ($r = Mysql::getInstance()->fetch_assoc($q)) {
		$t = '';
		if (!$head) {
		    foreach (array_keys($r) as $key) {
			$t .= Html::tag($key, 'td', array('class' => 'chead'));
		    }
		    $resultTable = Html::tr($t);
		    $t = '';
		    $head = true;
		}

		foreach ($r as $key => $val) {
		    $t .= Html::tag(htmlspecialchars(stripslashes($val)), 'td', array('class' => 'confclass'));
		}

		$resultTable .= Html::tr($t);
		$t = '';
	    }
	    if (!$head) {
		Adminarea::getInstance()->adminInfo(__('Запит виконано'));
	    } else {
		Aurus::getInstance()->addContent(Html::table($resultTable));
	    }
	}

	$c .= Aurus::getInstance()->form_start();
	$c .= '<div align="center"><textarea name="query" rows="5" cols="50" style="width:98%;height:200px">' . $_POST['query'] . '</textarea>';
	$c .= '<input type="submit" value="' . __('Здійснити запит') . '"/>';
	$c .= '</div>';
	$c .= '</form>';
	Aurus::getInstance()->addContent($c);
    }

}